package com.mollytask.institution;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.mollytask.R;
import com.mollytask.bean.DataFill;
import com.mollytask.bean.Institution;
import com.mollytask.bean.Message;
import com.mollytask.bean.MyUser;
import com.mollytask.util.RegexUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

public class DataFillActivity extends AppCompatActivity {

    private int year;
    private int quart;

    private EditText et_code, et_name, et_type, et_adress, et_post_code, et_email, et_phone, et_fuzeren, et_usenum, et_hotarea, et_coldarea, et_stati_name, et_stati_phone;

    private LinearLayout mBackLl;

    private Button mSubmit;

    private Institution mInstitution;

    @BindView(R.id.et_waterPrice)
    EditText et_waterPrice;
    @BindView(R.id.et_coalAmount)
    EditText et_coalAmount;
    @BindView(R.id.et_coalPrice)
    EditText et_coalPrice;
    @BindView(R.id.et_elecAmount)
    EditText et_elecAmount;
    @BindView(R.id.et_elecPrice)
    EditText et_elecPrice;
    @BindView(R.id.et_gasAmount)
    EditText et_gasAmount;
    @BindView(R.id.et_gasoglineAmount)
    EditText et_gasoglineAmount;
    @BindView(R.id.et_gasolinePrice)
    EditText et_gasolinePrice;
    @BindView(R.id.et_gasPrice)
    EditText et_gasPrice;
    @BindView(R.id.et_waterAmount)
    EditText et_waterAmount;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data_fill);
        ButterKnife.bind(this);
        year = getIntent().getIntExtra("year", 2017);
        quart = getIntent().getIntExtra("quart", 1);

        initView();
        initData();
        setListener();

    }


    private void initView() {
        mBackLl = (LinearLayout) findViewById(R.id.ll_back);
        et_code = (EditText) findViewById(R.id.et_code);
        et_name = (EditText) findViewById(R.id.et_name);
        et_type = (EditText) findViewById(R.id.et_type);
        et_adress = (EditText) findViewById(R.id.et_adress);
        et_post_code = (EditText) findViewById(R.id.et_post_code);
        et_email = (EditText) findViewById(R.id.et_email);
        et_phone = (EditText) findViewById(R.id.et_phone);
        et_fuzeren = (EditText) findViewById(R.id.et_fuzeren);
        et_usenum = (EditText) findViewById(R.id.et_usenum);
        et_hotarea = (EditText) findViewById(R.id.et_hotarea);
        et_coldarea = (EditText) findViewById(R.id.et_coldarea);
        et_stati_name = (EditText) findViewById(R.id.et_stati_name);
        et_stati_phone = (EditText) findViewById(R.id.et_stati_phone);

        mSubmit = (Button) findViewById(R.id.btn_submit);
    }

    private void initData() {
        //得到当前用户
        MyUser myUser = BmobUser.getCurrentUser(MyUser.class);
        //查村数据库存储的信息
        BmobQuery<Institution> query = new BmobQuery<>();
        query.addWhereEqualTo("mInCode", myUser.getCode());
        query.findObjects(new FindListener<Institution>() {
            @Override
            public void done(List<Institution> list, BmobException e) {
                mInstitution = list.get(0);
                et_code.setText(mInstitution.getmInCode());
                et_name.setText(mInstitution.getmInName());
                et_type.setText(mInstitution.getmInType());
                et_adress.setText(mInstitution.getmInAddress());
                et_post_code.setText(mInstitution.getmInPostCode());
                et_email.setText(mInstitution.getmInEmail());
                et_phone.setText(mInstitution.getmInPaxPhone());
                et_fuzeren.setText(mInstitution.getmInDirector());
            }
        });

    }


    private void setListener() {
        mBackLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //点击下一步
        mSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean cancel = false;

                if (TextUtils.isEmpty(et_usenum.getText().toString())
                        || TextUtils.isEmpty(et_hotarea.getText().toString())
                        || TextUtils.isEmpty(et_coldarea.getText().toString())
                        || TextUtils.isEmpty(et_stati_name.getText().toString())
                        || TextUtils.isEmpty(et_stati_phone.getText().toString())
                        || TextUtils.isEmpty(et_waterAmount.getText().toString())
                        || TextUtils.isEmpty(et_waterPrice.getText().toString())
                        || TextUtils.isEmpty(et_coalAmount.getText().toString())
                        || TextUtils.isEmpty(et_coalPrice.getText().toString())
                        || TextUtils.isEmpty(et_elecAmount.getText().toString())
                        || TextUtils.isEmpty(et_elecPrice.getText().toString())
                        || TextUtils.isEmpty(et_gasAmount.getText().toString())
                        || TextUtils.isEmpty(et_gasPrice.getText().toString())
                        || TextUtils.isEmpty(et_gasoglineAmount.getText().toString())
                        || TextUtils.isEmpty(et_gasolinePrice.getText().toString())) {
                    cancel = true;
                    Toast.makeText(DataFillActivity.this, "请输入完整信息", Toast.LENGTH_LONG).show();
                } else if (!RegexUtil.checkMobile(et_stati_phone.getText().toString())) {
                    Toast.makeText(DataFillActivity.this, "手机格式不正确", Toast.LENGTH_LONG).show();
                    cancel = true;
                }
                //格式正确
                if (!cancel) {
                    DataFill dataFill = new DataFill();

                    dataFill.setmFillId(mInstitution.getmInCode() + year + quart);
                    dataFill.setmInId(mInstitution.getmInCode());
                    dataFill.setmInName(mInstitution.getmInName());
                    dataFill.setmSuperCode(mInstitution.getmInSuperAccount());
                    dataFill.setmConsumerNo(et_usenum.getText().toString());
                    dataFill.setmHeatArea(et_hotarea.getText().toString());
                    dataFill.setmCoolArea(et_coldarea.getText().toString());
                    dataFill.setElecPrice(et_elecPrice.getText().toString());
                    dataFill.setElecAmount(et_elecAmount.getText().toString());
                    dataFill.setWaterPrice(et_waterPrice.getText().toString());
                    dataFill.setWaterAmount(et_waterAmount.getText().toString());
                    dataFill.setCoalPrice(et_coalPrice.getText().toString());
                    dataFill.setCoalAmount(et_coalAmount.getText().toString());
                    dataFill.setGasPrice(et_gasPrice.getText().toString());
                    dataFill.setGasAmount(et_gasAmount.getText().toString());
                    dataFill.setGasolinePrice(et_gasolinePrice.getText().toString());
                    dataFill.setGasoglineAmount(et_gasoglineAmount.getText().toString());
                    dataFill.setmStatisticianName(et_stati_name.getText().toString());
                    dataFill.setmStatisticianTel(et_stati_phone.getText().toString());
                    dataFill.setStatus(0);

                    dataFill.save(new SaveListener<String>() {
                        @Override
                        public void done(String s, BmobException e) {
                            if (e == null) {
                                Toast.makeText(DataFillActivity.this, "提交成功", Toast.LENGTH_LONG).show();

                                Message message = new Message();
                                message.setmFromCode(mInstitution.getmInCode());
                                message.setmToCode(mInstitution.getmInSuperAccount());
                                message.setType(1);
                                message.setmContent(mInstitution.getmInName() + "提交了" + dataFill.getmFillId().substring(9, 13) + "年第" + dataFill.getmFillId().substring(13, 14) + "季度的数据，请注意查看");
                                message.save(new SaveListener<String>() {
                                    @Override
                                    public void done(String s, BmobException e) {

                                    }
                                });

                            } else {
                                Toast.makeText(DataFillActivity.this, "提交失败" + e.getMessage().toString(), Toast.LENGTH_LONG).show();
                            }
                        }
                    });
                    finish();

                }
            }
        });
    }

}
